package com.example.analyzevisio.service;

import com.example.analyzevisio.model.ServiceNode;

import com.example.analyzevisio.model.component.Items;
import com.example.analyzevisio.model.component.Position;
import com.example.analyzevisio.model.component.Size;
import com.example.analyzevisio.model.serviceNode.*;

import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.analyzevisio.utis.Obtain.*;

/**
 * 服务节点
 */
public class ServiceNodeService {
    private static HashMap<String, Map<String, String>> dataMap;

    public void setDataMap(HashMap<String, Map<String, String>> dataMap){
        this.dataMap = dataMap;
    }

    public static ServiceNode getServiceNode(Element shapeElement, String text) {
        ServiceNode serviceNode = new ServiceNode();


        //获取坐标和父节点
        Map<String, String> obtainCoordinates = getObtainCoordinates(shapeElement);
        double pinX = Double.parseDouble(obtainCoordinates.get("pinX"));
        double pinY = Double.parseDouble(obtainCoordinates.get("pinY"));

        serviceNode.setPosition(new Position(pinX,pinY));
        serviceNode.setParent(obtainCoordinates.get("parent"));
        Size size = new Size(
                80,
                25
        );
        serviceNode.setSize(size);
        serviceNode.setView("html-view");
        serviceNode.setShape("html");

        String uniqueID = shapeElement.getAttribute("UniqueID");
        String substring = uniqueID.substring(1, uniqueID.length() - 1);
        serviceNode.setId(substring);

        ServiceData serviceData = new ServiceData();
        serviceData.setName(text);
        serviceData.setType("node");
        serviceData.setLabel("ServiceTask1");

        String id = shapeElement.getAttribute("ID");
        serviceData.setStateId(dataMap.get(id).get("stateId"));
        serviceData.setStateType("ServiceTask");

        StateProps stateProps = new StateProps();
        stateProps.setServiceName("SlpProxyInvoker");
        stateProps.setServiceMethod("invoke");
        stateProps.set_temp_service_type_chinese_name("产品服务");

        ArrayList<Input> inputArrayList = new ArrayList<>();
        Input input = new Input();
        input.set_conductor_service_name_key_("");
        input.set_conductor_service_method_key_("");
        input.set_conductor_service_version_key_("");
        input.set_conductor_service_type_key_("");
        input.set_conductor_level_key_("0");
        input.set_business_key_("$Sequence.BUSINESS_KEY|SIMPLE");
        inputArrayList.add(input);

        stateProps.setInput(inputArrayList);
        stateProps.setOutput(null);

        HashMap<String, String> statusMap = new HashMap<>();
        statusMap.put("$Exception{java.lang.Exception}","UN");
        statusMap.put("[sysHead][retCd] == '000000'","SU");
        statusMap.put("[sysHead][retCd] != '000000'","FA");
        stateProps.setStatus(statusMap);

        ArrayList<String> list = new ArrayList<>();
        stateProps.setRetry(list);

        serviceData.setStateProps(stateProps);

        serviceNode.setData(serviceData);


        Ports ports = new Ports();
        Groups groups = new Groups();
        Group1 group1 = new Group1();
        Attrs attrs = new Attrs();
        Circle circle = new Circle();
        circle.setR("3");
        circle.setMagnet(true);
        circle.setStroke("#31d0c6");
        circle.setStrokeWidth("2");
        circle.setFill("#fff");
        HashMap<String, String> map = new HashMap<>();
        map.put("visibility","hidden");
        circle.setStyle(map);
        attrs.setCircle(circle);

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("name","absolute");
        group1.setAttrs(attrs);
        group1.setPosition(hashMap);
        groups.setGroup1(group1);

        List<Items> Items = getConnectionPoint("服务");

        ports.setGroups(groups);
        ports.setItems(Items);

        serviceNode.setPorts(ports);

        serviceNode.setZIndex("10");

        return serviceNode;
    }
}
